package com.ruoyi.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * OSS文件上传工具类
 */
public class OssUtils {

    //Oss服务器地址
    private static String endpoint = "oss-cn-wuhan-lr.aliyuncs.com";
    //密钥
    private static String accessKeyId = "LTAI5t7xuQcFcnkRygVESZ5L";
    private static String accessKeySecret = "a4xW0rtLrulDePhXqlQC0HvLbZMtN2";
    //Oss服务器名
    private static String bucketName = "medical-registration-imgs";
    //bucket中的目录名
    public static String dir = "images";

    /**
     * 文件上传
     */
    public static void upload(InputStream inputStream, String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        oss.putObject(bucketName, dir + "/" + fileName, inputStream);
        oss.shutdown();
    }

    /**
     * 获得文件URL
     */
    public static String getURL(String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //设置过期时间
        LocalDateTime time = LocalDateTime.now().plusDays(100);
        Date expiration = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        URL url = oss.generatePresignedUrl(bucketName, dir + "/" + fileName, expiration);
        oss.shutdown();
        return url.toString();
    }


    /**
     * 多文件上传
     *
     * @param inputStreams
     * @param fileNames
     */
    public static void uploads(List<InputStream> inputStreams, List<String> fileNames) {
        if (inputStreams.size() != fileNames.size()) {
            throw new IllegalArgumentException("输入流和文件名必须具有相同的大小");
        }

        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            for (int i = 0; i < inputStreams.size(); i++) {
                InputStream inputStream = inputStreams.get(i);
                String fileName = fileNames.get(i);
                // 使用PutObjectRequest上传
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dir + "/" + fileName, inputStream);
                PutObjectResult putObjectResult = oss.putObject(putObjectRequest);

                // 处理上传结果
                System.out.println("File: " + fileName + " ETag: " + putObjectResult.getETag() + " LastModified: " + putObjectResult.getResponse());
            }
        } finally {
            oss.shutdown();
        }
    }

    /**
     * 获取文件的URL
     *
     * @param fileName 文件名
     * @return 文件的URL
     */
    public static String getURLs(String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 设置过期时间
            LocalDateTime time = LocalDateTime.now().plusDays(100);
            Date expiration = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());

            // 生成 URL
            URL url = oss.generatePresignedUrl(bucketName, dir + "/" + fileName, expiration);
            return url.toString();
        } finally {
            oss.shutdown();
        }
    }

//    public static void main(String[] args) {
//        // 假设有两个文件名为 "file1.txt" 和 "file2.txt" 的文件，它们对应的 InputStream 放在一个 List 中
//        List<String> fileNames = Arrays.asList("file1.txt", "file2.txt");
//        List<InputStream> inputStreams = getInputStreams(); // 请替换为实际获取 InputStream 的方法
//        // 调用上传方法
//        uploads(inputStreams, fileNames);
//        // 获取文件URL示例
//        for (String fileName : fileNames) {
//            String url = getURLs(fileName);
//            System.out.println("文件路径: " + url);
//        }
//    }

    // 这是一个示例方法，用于获取实际的 InputStream 列表，请根据实际情况替换为你的方法
    private static List<InputStream> getInputStreams() {
        // 实际的获取 InputStream 的逻辑
        // 示例中直接返回一个空的 List，请根据需要修改为实际逻辑
        List<InputStream> inputStreams = new ArrayList<>();
        // 添加示例 InputStream，实际中请根据需要添加相应的 InputStream
        inputStreams.add(new ByteArrayInputStream("Hello, World!".getBytes()));
        inputStreams.add(new ByteArrayInputStream("Another file content.".getBytes()));
        return inputStreams;
    }


}